package cn.com.okgotest.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import cn.com.okgo.OkGo;
import cn.com.okgo.callback.HttpStringCallback;
import cn.com.okgo.cookie.CookiesManager;
import cn.com.okgo.https.HttpsUtils;
import cn.com.okgo.https.SafeHostnameVerifier;
import cn.com.okgo.https.SafeTrustManager;
import cn.com.okgotest.R;
import cn.com.okgotest.fragment.base.BaseFragment;
import okhttp3.OkHttpClient;

public class HttpsFragment extends BaseFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_https, container, false);
        init(view);
        return view;
    }

    private void init(View view) {

        final TextView textView=view.findViewById(R.id.textView);

        Button button1 = view.findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initOkGo();
            }
        });

        Button button = view.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                 //https://github.com/jeasonlzy
                //https://kyfw.12306.cn/otn
                textView.setText("......");
                OkGo.get("https://10.19.48.156/")
                        .execute(new HttpStringCallback() {
                            @Override
                            public void onSuccess(String obj, String baseUrl) {
                                textView.setText("成功: \n"+obj);
                            }

                            @Override
                            public void onFailed(Exception e, String baseUrl) {
                                textView.setText("失败: \n"+e.toString());
                            }
                        });

            }
        });
    }

    private void initOkGo() {
        Toast.makeText(getActivity(), "初始化 OkGo", Toast.LENGTH_SHORT).show();
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(5, TimeUnit.SECONDS);
        builder.readTimeout(5, TimeUnit.SECONDS);
        builder.writeTimeout(5, TimeUnit.SECONDS);
        builder.cookieJar(new CookiesManager());

        //https相关设置，以下几种方案根据需要自己设置

//        //方法一：信任所有证书,不安全有风险
//        HttpsUtils.SSLParams sslParams1 = HttpsUtils.getSslSocketFactory();
//        //方法二：自定义信任规则，校验服务端证书
//        HttpsUtils.SSLParams sslParams2 = HttpsUtils.getSslSocketFactory(new SafeTrustManager());

        //方法三：使用预埋证书，校验服务端证书（自签名证书）
        HttpsUtils.SSLParams sslParams3 = null;
        try {
            sslParams3 = HttpsUtils.getSslSocketFactory(getActivity().getAssets().open("ca.cer"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //方法四：使用bks证书和密码管理客户端证书（双向认证），使用预埋证书，校验服务端证书（自签名证书）
        //HttpsUtils.SSLParams sslParams4 = HttpsUtils.getSslSocketFactory(getAssets().open("xxx.bks"), "123456", getAssets().open("yyy.cer"));

        builder.sslSocketFactory(sslParams3.sSLSocketFactory, sslParams3.trustManager);
        //配置https的域名匹配规则，详细看demo的初始化介绍，不需要就不要加入，使用不当会导致https握手失败
        builder.hostnameVerifier(new SafeHostnameVerifier());
        OkGo.getInstance().setOkHttpClient(builder.build());
    }
}
